<!DOCTYPE html>
<html lang="en" data-color-mode="auto" data-light-theme="light" data-dark-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>G4F - Configuration</title>
    <link rel="apple-touch-icon" sizes="180x180" href="/dist/img/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/dist/img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/dist/img/favicon-16x16.png">
    <link rel="manifest" href="/dist/img/site.webmanifest">
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/light-74231a1f3bbb.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/dark-8a995f0bacd4.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-primitives-225433424a87.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-b8b91660c29d.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-205098e9fedd.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/code-177d21388df8.css" />
    <style>
        :root {
            --colour-1: #000000;
            --colour-2: #ccc;
            --colour-3: #e4d4ff;
            --colour-4: #f0f0f0;
            --colour-5: #181818;
            --colour-6: #242424;
            --accent: #8b3dff;
            --gradient: #1a1a1a;
            --background: #16101b;
            --size: 70vw;
            --top: 50%;
            --blur: 40px;
            --opacity: 0.6;
        }

        /* Body and text color */
        body {
            height: 100vh;
            margin: 0;
            padding: 0;
        }

        .hidden {
            display: none;
        }

        .container-lg {
            margin: 0 auto;
            padding: 8px;
        }

        @media only screen and (min-width: 40em) {
            .container-lg {
                max-width: 84%;
            }
        }
    </style>
</head>
<body>
    <article class="markdown-body entry-content container-lg" itemprop="text"><div class="markdown-heading"><h2 class="heading-element">G4F - Configuration</h2><a id="user-content-g4f---configuration" class="anchor" aria-label="Permalink: G4F - Configuration" href="#g4f---configuration"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h2 class="heading-element">Table of Contents</h2><a id="user-content-table-of-contents" class="anchor" aria-label="Permalink: Table of Contents" href="#table-of-contents"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ul>
<li><a href="#authentication">Authentication</a></li>
<li><a href="#cookies-configuration">Cookies Configuration</a></li>
<li><a href="#har-and-cookie-files">HAR and Cookie Files</a></li>
<li><a href="#debug-mode">Debug Mode</a></li>
<li><a href="#proxy-configuration">Proxy Configuration</a></li>
</ul>
<div class="markdown-heading"><h4 class="heading-element">Authentication</h4><a id="user-content-authentication" class="anchor" aria-label="Permalink: Authentication" href="#authentication"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Refer to the <a href="authentication.html">G4F Authentication Setup Guide</a> for detailed instructions on setting up authentication.</p>
<div class="markdown-heading"><h3 class="heading-element">Cookies Configuration</h3><a id="user-content-cookies-configuration" class="anchor" aria-label="Permalink: Cookies Configuration" href="#cookies-configuration"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Cookies are essential for using Meta AI and Microsoft Designer to create images.
Additionally, cookies are required for the Google Gemini and WhiteRabbitNeo Provider.
From Bing, ensure you have the "_U" cookie, and from Google, all cookies starting with "__Secure-1PSID" are needed.</p>
<p><strong>You can pass these cookies directly to the create function or set them using the <code>set_cookies</code> method before running G4F:</strong></p>
<div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">cookies</span> <span class="pl-k">import</span> <span class="pl-s1">set_cookies</span>

<span class="pl-en">set_cookies</span>(<span class="pl-s">".bing.com"</span>, {
  <span class="pl-s">"_U"</span>: <span class="pl-s">"cookie value"</span>
})

<span class="pl-en">set_cookies</span>(<span class="pl-s">".google.com"</span>, {
  <span class="pl-s">"__Secure-1PSID"</span>: <span class="pl-s">"cookie value"</span>
})</pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element">HAR and Cookie Files</h3><a id="user-content-har-and-cookie-files" class="anchor" aria-label="Permalink: HAR and Cookie Files" href="#har-and-cookie-files"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>Using .har and Cookie Files</strong>
You can place <code>.har</code> and cookie files <code>.json</code> in the default <code>./har_and_cookies</code> directory. To export a cookie file, use the <a href="https://chromewebstore.google.com/detail/editthiscookie-v3/ojfebgpkimhlhcblbalbfjblapadhbol" rel="nofollow">EditThisCookie Extension</a> available on the Chrome Web Store.</p>
<p><strong>Creating .har Files to Capture Cookies</strong>
To capture cookies, you can also create <code>.har</code> files. For more details, refer to the next section.</p>
<div class="markdown-heading"><h3 class="heading-element">Changing the Cookies Directory and Loading Cookie Files in Python</h3><a id="user-content-changing-the-cookies-directory-and-loading-cookie-files-in-python" class="anchor" aria-label="Permalink: Changing the Cookies Directory and Loading Cookie Files in Python" href="#changing-the-cookies-directory-and-loading-cookie-files-in-python"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>You can change the cookies directory and load cookie files in your Python environment. To set the cookies directory relative to your Python file, use the following code:</strong></p>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">os</span>.<span class="pl-s1">path</span>
<span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">cookies</span> <span class="pl-k">import</span> <span class="pl-s1">set_cookies_dir</span>, <span class="pl-s1">read_cookie_files</span>

<span class="pl-k">import</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">debug</span>
<span class="pl-s1">g4f</span>.<span class="pl-c1">debug</span>.<span class="pl-c1">logging</span> <span class="pl-c1">=</span> <span class="pl-c1">True</span>

<span class="pl-s1">cookies_dir</span> <span class="pl-c1">=</span> <span class="pl-s1">os</span>.<span class="pl-c1">path</span>.<span class="pl-c1">join</span>(<span class="pl-s1">os</span>.<span class="pl-c1">path</span>.<span class="pl-c1">dirname</span>(<span class="pl-s1">__file__</span>), <span class="pl-s">"har_and_cookies"</span>)
<span class="pl-en">set_cookies_dir</span>(<span class="pl-s1">cookies_dir</span>)
<span class="pl-en">read_cookie_files</span>(<span class="pl-s1">cookies_dir</span>)</pre></div>
<div class="markdown-heading"><h3 class="heading-element">Debug Mode</h3><a id="user-content-debug-mode" class="anchor" aria-label="Permalink: Debug Mode" href="#debug-mode"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>If you enable debug mode, you will see logs similar to the following:</strong></p>
<pre><code>Read .har file: ./har_and_cookies/you.com.har
Cookies added: 10 from .you.com
Read cookie file: ./har_and_cookies/google.json
Cookies added: 16 from .google.com
</code></pre>
<div class="markdown-heading"><h4 class="heading-element">.HAR File for OpenaiChat Provider</h4><a id="user-content-har-file-for-openaichat-provider" class="anchor" aria-label="Permalink: .HAR File for OpenaiChat Provider" href="#har-file-for-openaichat-provider"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h5 class="heading-element">Generating a .HAR File</h5><a id="user-content-generating-a-har-file" class="anchor" aria-label="Permalink: Generating a .HAR File" href="#generating-a-har-file"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>To utilize the OpenaiChat provider, a .har file is required from <a href="https://chatgpt.com/" rel="nofollow">https://chatgpt.com/</a>. Follow the steps below to create a valid .har file:</strong></p>
<ol>
<li>Navigate to <a href="https://chatgpt.com/" rel="nofollow">https://chatgpt.com/</a> using your preferred web browser and log in with your credentials.</li>
<li>Access the Developer Tools in your browser. This can typically be done by right-clicking the page and selecting "Inspect," or by pressing F12 or Ctrl+Shift+I (Cmd+Option+I on a Mac).</li>
<li>With the Developer Tools open, switch to the "Network" tab.</li>
<li>Reload the website to capture the loading process within the Network tab.</li>
<li>Initiate an action in the chat which can be captured in the .har file.</li>
<li>Right-click any of the network activities listed and select "Save all as HAR with content" to export the .har file.</li>
</ol>
<div class="markdown-heading"><h5 class="heading-element">Storing the .HAR File</h5><a id="user-content-storing-the-har-file" class="anchor" aria-label="Permalink: Storing the .HAR File" href="#storing-the-har-file"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ul>
<li>Place the exported .har file in the <code>./har_and_cookies</code> directory if you are using Docker. Alternatively, if you are using Python from a terminal, you can store it in a <code>./har_and_cookies</code> directory within your current working directory.</li>
</ul>
<blockquote>
<p><strong>Note:</strong> Ensure that your .har file is stored securely, as it may contain sensitive information.</p>
</blockquote>
<div class="markdown-heading"><h3 class="heading-element">Proxy Configuration</h3><a id="user-content-proxy-configuration" class="anchor" aria-label="Permalink: Proxy Configuration" href="#proxy-configuration"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>If you want to hide or change your IP address for the providers, you can set a proxy globally via an environment variable:</strong></p>
<p><strong>- On macOS and Linux:</strong></p>
<div class="highlight highlight-source-shell"><pre><span class="pl-k">export</span> G4F_PROXY=<span class="pl-s"><span class="pl-pds">"</span>http://host:port<span class="pl-pds">"</span></span></pre></div>
<p><strong>- On Windows:</strong></p>
<div class="highlight highlight-source-shell"><pre><span class="pl-c1">set</span> G4F_PROXY=http://host:port</pre></div>
<hr>
<p><a href="/docs/">Return to Documentation</a></p>
</article>
</body>
</html>